package com.sc.nft.admin.controller;




import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.vo.PackageRulePageVO;
import com.sc.nft.admin.vo.PackageSimpleVO;
import com.sc.nft.entity.vo.WriteOffRecordVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.PackageRule;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.service.PackageRecordService;
import com.sc.nft.service.PackageRuleService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 套餐后台管理控制器
 *
 * @author 张翔宇
 * @since 2023年10月14日
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/package")
@Api(tags = "后台商家套餐模块")
public class PackageAdminController {

    private final PackageRuleService packageRuleService;
    private final PackageRecordService packageRecordService;
    private final DigitalCollectionDao digitalCollectionDao;
    private final UserCreatorInfoDao userCreatorInfoDao;


    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建套餐", notes = "创建套餐", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result create(@Valid @RequestBody PackageAddDTO packageAddDTO) {
        DigitalCollection digitalCollection = digitalCollectionDao.getById(packageAddDTO.getCollectionId());
        if(ObjectUtil.isNull(digitalCollection)){
            return Result.fail(ErrorCode.COLLECTION_NOT_FOUND);
        }
        GetDaoByIdVO dao = userCreatorInfoDao.getDaoById(digitalCollection.getUserCreatorId());
        if(ObjectUtil.isNull(dao)){
            return Result.fail("未找到DAO组织");
        }
        PackageRule packageRule = packageRuleService.getByCollectionId(packageAddDTO.getCollectionId());
        if(ObjectUtil.isNotNull(packageRule)){
            return Result.fail("藏品已配置过套餐");
        }
        packageRule = BeanUtil.toBean(packageAddDTO, PackageRule.class);
        packageRule.setDaoId(digitalCollection.getUserCreatorId());
        packageRule.insert();
        return Result.success();
    }


    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑套餐", notes = "编辑套餐", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody PackageEditDTO packageEditDTO) {
        PackageRule packageRule = packageRuleService.getById(packageEditDTO.getId());
        packageRule.setPackageName(packageEditDTO.getPackageName());
        packageRule.setStartTime(packageEditDTO.getStartTime());
        packageRule.setEndTime(packageEditDTO.getEndTime());
        packageRule.setStatus(packageEditDTO.getStatus());
        packageRule.setPackageDescription(packageEditDTO.getPackageDescription());
        packageRule.updateById();
        return Result.success();
    }


    @PostMapping(value = "/remove", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除套餐", notes = "删除套餐", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result remove(@Valid @RequestBody IdRequest request) {
        PackageRule packageRule = packageRuleService.getById(request.getId());
        if(ObjectUtil.isNull(packageRule)){
            return Result.fail("未找到套餐");
        }
        if(packageRecordService.writeOffCount(packageRule.getId())>0){
            return Result.fail("已有核销数据，不允许删除");

        }
        packageRule.deleteById();
        return Result.success();
    }


    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "套餐列表", notes = "套餐列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PackageRulePageVO> page(@Valid @RequestBody PackageQueryDTO queryDTO) {
        Page<PackageRule> adminPage = packageRuleService.getAdminPage(queryDTO.toMp(), queryDTO.getPackageName(), queryDTO.getCollectionId(),queryDTO.getStartTime(),queryDTO.getEndTime());
        Page<PackageRulePageVO> result = new Page<>(adminPage.getCurrent(),adminPage.getSize(),adminPage.getTotal());
        List<PackageRulePageVO> list = Lists.newArrayList();
        for (PackageRule record : adminPage.getRecords()) {
            PackageRulePageVO vo = new PackageRulePageVO();
            vo.setId(record.getId());
            vo.setCollectionId(record.getCollectionId());
            vo.setFirstTitle(digitalCollectionDao.getFirstTitleById(record.getCollectionId()));
            vo.setStartTime(record.getStartTime());
            vo.setEndTime(record.getEndTime());
            vo.setWriteOffCount(packageRecordService.writeOffCount(record.getId()));
            vo.setPackageName(record.getPackageName());
            vo.setPackageDescription(record.getPackageDescription());
            vo.setStatus(record.getStatus());
            vo.setCreateTime(record.getCreateTime());
            list.add(vo);
        }
        result.setRecords(list);
        return PageResult.success(result);
    }

    @PostMapping(value = "/writeOffRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "核销记录", notes = "核销记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<WriteOffRecordVO> writeOffRecord(@Valid @RequestBody WriteOffRecordDTO queryDTO) {
        return PageResult.success(packageRecordService.getPage(queryDTO.toMp(),queryDTO.getPackageId(),queryDTO.getUserTel(),queryDTO.getOperationUserTel(),queryDTO.getStartRecordTime(),queryDTO.getEndRecordTime()) );
    }


    @PostMapping(value = "/simpleList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单列表", notes = "简单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<PackageSimpleVO>> simpleList( ) {
        return Result.successColl(packageRuleService.simpleList(), PackageSimpleVO.class);
    }

}
